package xin.petstore.framework.cipher;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5PasswordEncoder {
    public String encodePassword(String rawPass, Object salt)
            throws NoSuchAlgorithmException {
        String saltedPass = mergePasswordAndSalt(rawPass, salt, false);
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] digest = messageDigest.digest(encode(saltedPass));

        for (int i = 1; i < 1; i++) {
            digest = messageDigest.digest(digest);
        }
        return new String(Hex.encode(digest));
    }

    protected String mergePasswordAndSalt(String password, Object salt, boolean strict) {
        if (password == null) {
            password = "";
        }

        if ((strict) && (salt != null) && (
                (salt.toString().lastIndexOf("{") != -1) ||
                        (salt.toString().lastIndexOf("}") != -1))) {
            throw new IllegalArgumentException(
                    "Cannot use { or } in salt.toString()");
        }

        if ((salt == null) || ("".equals(salt))) {
            return password;
        }
        return password + "{" + salt.toString() + "}";
    }

    public static byte[] encode(CharSequence string) {
        Charset CHARSET = Charset.forName("UTF-8");
        try {
            ByteBuffer bytes = CHARSET.newEncoder().encode(
                    CharBuffer.wrap(string));
            byte[] bytesCopy = new byte[bytes.limit()];
            System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit());

            return bytesCopy;
        } catch (CharacterCodingException e) {
        }
        throw new IllegalArgumentException("Encoding failed", e);
    }
}

